﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ODP.Com;
using ODP.Enum;
using ODP.Model;
using ODP.Helper;
using System.Text;

namespace ODP.Web.Controllers
{
    public class SinaController : Controller
    {
        //
        // GET: /Sina/

        public ActionResult Index()
        {
            SinaAPI.SinaOAuth Qoa = new SinaAPI.SinaOAuth();
            List<QueryStringParameter> parameters = new List<QueryStringParameter>();
            string queryString = "";
            string requestUrl = "";

            Qoa.callbackUrl = "http://127.0.0.1:3573/sina/default";
            Qoa.customKey = "2159118604";
            Qoa.customSecret = "43af6a039a41e30b93c12b0cb4169429";
            parameters.Add(new QueryStringParameter("response_type", "code"));
            Qoa.GetOauthUrl(Qoa.Request_token_url, HttpMethod.GET.ToString(), parameters, out queryString,out requestUrl);

            Current.OAuth = new SinaAPI.SinaOAuth(Qoa);
            Current.tType = Qoa.twitterType;

            return Redirect(requestUrl + "?" + queryString);
        }

        public ActionResult Default()
        {

            if (string.IsNullOrEmpty(Current.OAuth.verify) || string.IsNullOrEmpty(Current.OAuth.tokenKey))
            {

                string requestString = "";
                string request_Url = "";

                List<QueryStringParameter> parameters = new List<QueryStringParameter>();


                Current.OAuth.Access_token_url = "https://api.weibo.com/oauth2/access_token";
                Current.OAuth.callbackUrl = "http://127.0.0.1:3573/sina/default";
                Current.OAuth.verify = this.Request.QueryString["code"];
                parameters.Add(new QueryStringParameter("grant_type", "authorization_code"));
                parameters.Add(new QueryStringParameter("client_secret", Current.OAuth.customSecret));

                Current.OAuth.GetOauthUrl(Current.OAuth.Access_token_url, HttpMethod.POST.ToString(), parameters, out requestString, out request_Url);
                string result = new SyncHttp().HttpPost(request_Url, requestString);


                Current.OAuth.tokenKey = ODP.Helper.JsonHelper.GetValueByName(result,"access_token");
                Current.OAuth.Uid = ODP.Helper.JsonHelper.GetValueByName(result, "uid");
            }



            var resultJson = ((Newtonsoft.Json.Linq.JObject)(test()));
            StringBuilder sb = new StringBuilder();
            foreach (var item in resultJson)
            {
                var temp = item.Value as Newtonsoft.Json.Linq.JObject;
                if (temp != null)
                {
                    foreach (var tempitem in temp)
                    {
                        sb.Append(tempitem.Value + "</ br>");
                    }
                }
                else
                {
                    sb.Append(item.Value + "</ br>");
                }
            }
            ViewData["result"] = sb.ToString();
            return View();
        }

        public void GetValues(StringBuilder sb,Newtonsoft.Json.Linq.JObject jo)
        {
            if (jo.HasValues)
            {

            }
            else
            {
               
            }
        }


        public object test()
        {
            StringBuilder result = new StringBuilder();
            result.Append("{\"trends\":{");
            result.Append("\"2010-08-03 07:20\":");
            result.Append("[");
            result.Append("{");
            result.Append("\"name\":\"记忆犹新\",");
            result.Append("\"query\":\"记忆犹新\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"金希澈\",");
            result.Append("\"query\":\"金希澈\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"希澈\",");
            result.Append("\"query\":\"希澈\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"李晟敏\",");
            result.Append("\"query\":\"李晟敏\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"刘惜君\",");
            result.Append("\"query\":\"刘惜君\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"陈文茜\",");
            result.Append("\"query\":\"陈文茜\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"理想型\",");
            result.Append("\"query\":\"理想型\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"晚霞\",");
            result.Append("\"query\":\"晚霞\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"洗碗\",");
            result.Append("\"query\":\"洗碗\",");
            result.Append("},");
            result.Append("{");
            result.Append("\"name\":\"狗叫\",");
            result.Append("\"query\":\"狗叫\",");
            result.Append("}");
            result.Append("]},");
            result.Append("as_of:1280833537,");
            result.Append("}");

            return JsonHelper.GetJsonObject(result.ToString());
        }
    }
}
